﻿using BLL.Interfaces;
using DAL.DBInteractions;
using POCOS;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class ArticlesServcices : IArticlesServices
    {
        private readonly IUnitOfWork _unitOfWork;

        public ArticlesServcices(IUnitOfWork unitofwork)
        {
            this._unitOfWork = unitofwork;
        }

      


        public IEnumerable<Article> GetArticle()
        {
            return _unitOfWork.ArticleRepository.GetAll();
           
        }

        public Article GetArticleById(int id)
        {
            return _unitOfWork.ArticleRepository.GetById(id);
           
        }

        public void CreateArticle(Article Article)
        {
        _unitOfWork.ArticleRepository.Add(Article);
        _unitOfWork.Commit();
        }

        public void UpdateArticle(Article Article)
        {
            _unitOfWork.ArticleRepository.Update(Article);
            _unitOfWork.Commit();
        }

        public void DeleteArticle(int id)
        {
            _unitOfWork.ArticleRepository.Delete(GetArticleById(id));
            _unitOfWork.Commit();
        }
        public Article Find(int id)
        {
            return _unitOfWork.ArticleRepository.Find(x => x.AuteurId == id).FirstOrDefault();
        }

        public Auteur FindAuthor(int id)
        {
            return _unitOfWork.AuteurRepository.FindAuthor(x => x.Id == id).FirstOrDefault();

        }




        public string GetAuthorName (int id)
        
        {


            return _unitOfWork.AuteurRepository.GetById(id).Name+ " " + _unitOfWork.AuteurRepository.GetById(id).Lastname;
            
        
        
        }

        #region ArticleJtable

        //grid get article list

        public List<Article> GetArticles(int startIndex, int count, string sorting)
        {
            IEnumerable<Article> query = _unitOfWork.ArticleRepository.GetAll();
               

            //Sorting
            //This ugly code is used just for demonstration.
            //Normally, Incoming sorting text can be directly appended to an SQL query.
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("Name ASC"))
            {
                query = query.OrderBy(p => p.Title);
            }
            else if (sorting.Equals("Name DESC"))
            {
                query = query.OrderByDescending(p => p.Title);
            }

            else if (sorting.Equals("Culture ASC"))
            {
                query = query.OrderBy(p => p.Culture);
            }
          
            else if (sorting.Equals("Culture DESC"))
            {
                query = query.OrderByDescending(p => p.Culture);
            }

           
            else if (sorting.Equals("AuteurId ASC"))
            {
                query = query.OrderBy(p => p.AuteurId);
            }

            else if (sorting.Equals("CreationDate ASC"))
            {
                query = query.OrderBy(p => p.CreationDate);
            }
            else if (sorting.Equals("CreationDate DESC"))
            {
                query = query.OrderByDescending(p => p.CreationDate);
            }
            else if (sorting.Equals("IsActive ASC"))
            {
                query = query.OrderBy(p => p.IsActive);
            }
            else if (sorting.Equals("IsActive DESC"))
            {
                query = query.OrderByDescending(p => p.IsActive);
            }


            else if (sorting.Equals("Article ASC"))
            {
                query = query.OrderBy(p => p.ArticleContenent);
            }
            else if (sorting.Equals("Article DESC"))
            {
                query = query.OrderByDescending(p => p.ArticleContenent);
            }



            else
            {
                query = query.OrderBy(p => p.Title); //Default!
            }

            return count > 0
                       ? query.Skip(startIndex).Take(count).ToList() //Paging
                       : query.ToList(); //No paging
        }


        public List<Article> GetArticlesByFilter(string date, int auteurid, int startIndex, int count, string sorting)
        {
            IEnumerable<Article> query = _unitOfWork.ArticleRepository.GetAll();

            //Filters



                if (!string.IsNullOrEmpty(date))
            {
                DateTime enteredDate ;


                if (DateTime.TryParse(date, out enteredDate))
                {
                query = query.Where(p => p.CreationDate >= enteredDate);
                }
            }

            if (auteurid > 0)
            {
                query = query.Where(p => p.AuteurId == auteurid);
            }

            //Sorting
            //This ugly code is used just for demonstration.
            //Normally, Incoming sorting text can be directly appended to an SQL query.
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("Name ASC"))
            {
                query = query.OrderBy(p => p.Title);
            }
            else if (sorting.Equals("Name DESC"))
            {
                query = query.OrderByDescending(p => p.Title);
            }
           
            else if (sorting.Equals("CityId ASC"))
            {
                query = query.OrderBy(p => p.AuteurId);
            }
            else if (sorting.Equals("CityId DESC"))
            {
                query = query.OrderByDescending(p => p.AuteurId);
            }
            else if (sorting.Equals("BirthDate ASC"))
            {
                query = query.OrderBy(p => p.CreationDate);
            }
            else if (sorting.Equals("BirthDate DESC"))
            {
                query = query.OrderByDescending(p => p.CreationDate);
            }
            else if (sorting.Equals("IsActive ASC"))
            {
                query = query.OrderBy(p => p.IsActive);
            }
            else if (sorting.Equals("IsActive DESC"))
            {
                query = query.OrderByDescending(p => p.IsActive);
            }
            else
            {
                query = query.OrderBy(p => p.Title); //Default!
            }

            return count > 0
                       ? query.Skip(startIndex).Take(count).ToList() //Paging
                       : query.ToList(); //No paging
        }


        public int GetArticleCountByFilter(string date, int id)
        {

        IEnumerable<Article> query = _unitOfWork.ArticleRepository.GetAll();
            //dat format 

            //Filters

      if (!string.IsNullOrEmpty(date))
            {
         // DateTime enteredDate = DateTime.Parse(date);
                DateTime dt= new DateTime();
                DateTime.TryParse(date,out dt);
     
          var result = query.Where(p => p.CreationDate >= dt);
                          
               }

            if (id > 0)
            {
                query = query.Where(p => p.AuteurId == id);
            }

            return query.Count();
        }

     
        public int GetArticleCount()
        {
            return _unitOfWork.ArticleRepository.GetAll().Count();

        }
        
        

 
    
        #endregion


        
        
        public void SaveArticle()
        {
             _unitOfWork.Commit();
        }
    }
}
